package web.servlets;

import domain.Account;
import domain.Cart;
import service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ViewIncludeAccountFieldsServlet extends HttpServlet {
    private static final String VIEW_INCLUDEACCOUNT="/WEB-INF/jsp/account/EditAccountForm.jsp";
    private static final String ERROR_MESSAGE="/WEB-INF/jsp/common/Error.jsp";
    private static final String VIEW_SIGNONFORM="/WEB-INF/jsp/account/SignonForm.jsp";
    private static final String MAIN = "/WEB-INF/jsp/catalog/Main.jsp";
    private String username;
    private String password;
    Account account =new Account();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AccountService accountService = new AccountService();
        username = req.getParameter("username");
        password = req.getParameter("password");
        String code=req.getParameter("vcode");
        Cart cart = null;
        HttpSession session = req.getSession();
        String vcode=(String) session.getAttribute("vcode");
        if (accountService.getAccount(username,password)==null||(!code.equalsIgnoreCase(vcode))){
            session.setAttribute("message","Failed! Your uername or password or verification code is wrong");
            req.getRequestDispatcher(ERROR_MESSAGE).forward(req,resp);
        }else {
           // if(isSame) {
                account = accountService.getAccount(username,password);
                req.setAttribute("account",account);
                session.setAttribute("username",username);

                session.setAttribute("account",account);
                session.setAttribute("cart",cart);
                req.getRequestDispatcher(MAIN).forward(req,resp);
//
    }
    }
    }

